#include "display.c"

void game_zone_remove_shape()
{
    uint temp = game_shape.shape;
    uchar i;
    for (i = 0; i < 4; i++)
    {
        // 从game_shape 的4x4方格最底下一层开始向上遍历
        GAME_ZONE[3 - i + game_shape.y] &= ~((temp & 0x000f) << (9 - game_shape.x));
        temp = temp >> 4;
    }
}

void game_zone_add_shape()
{
    uint temp = game_shape.shape;
    uchar i;
    for (i = 0; i < 4; i++)
    {
        // 从game_shape 的4x4方格最底下一层开始向上遍历
        GAME_ZONE[3 - i + game_shape.y] |= ((temp & 0x000f) << (9 - game_shape.x));
        temp = temp >> 4;
    }
}

/**
 * 返回1：方块可以放置到当前位置
 * 返回0：方块不能防止到当前位置
 */
bit game_check_shape_placed(uchar x, uchar y, uint shape)
{
    uint temp = shape;
    uchar i;
    for (i = 0; i < 4; i++)
    {
        // printf("%x,%x\n", ((temp & 0x000f) << (9 - x)),
        //        GAME_ZONE[3 - i + y]);
        if ((((temp & 0x000f) << (9 - x)) & GAME_ZONE[3 - i + y]) != 0x0000)
        {
            return 0;
        }
        temp = temp >> 4;
    }
    return 1;
}

uchar game_rand()
{
    game_rand_seed = (game_rand_seed * 12345 + 67890) % 65535;
    return game_rand_seed;
}

void game_init_shape()
{
    game_shape.shape_type = game_rand() % 7;
    game_shape.shape_pos = game_rand() % 4;
    game_shape.x = 3;
    game_shape.y = 0;
    game_shape.shape = TETRIS_SHAPES[game_shape.shape_type][game_shape.shape_pos];
    game_shape.next_type = game_rand() % 7;
    game_shape.next_pos = game_rand() % 4;
}

void game_next_shape()
{
    game_shape.shape_type = game_shape.next_type;
    game_shape.shape_pos = game_shape.next_pos;
    game_shape.x = 3;
    game_shape.y = 0;
    game_shape.shape = TETRIS_SHAPES[game_shape.shape_type][game_shape.shape_pos];
    game_shape.next_type = game_rand() % 7;
    game_shape.next_pos = game_rand() % 4;
}

void game_show_next_shape()
{
    uint shape = TETRIS_SHAPES[game_shape.next_type][game_shape.next_pos];
    uchar two_box_display_data[] = {0x00, 0x00, 0x00, 0x00};
    uchar i;
    uchar page = 6, col = 8;

    for (i = 0; i < 4; i++)
    {
        // 高8位做4次，显示到page区域，每次写入4byte
        display_data_fill_two_box(two_box_display_data, (shape >> (15 - i)) & 0x0001 == 1,
                                  (shape >> (11 - i)) & 0x0001 == 1);
        lcd_display_bytes_in_page(page, col + i * 4, two_box_display_data, sizeof(two_box_display_data));
        // 低8位做4次，显示到page+1区域
        display_data_fill_two_box(two_box_display_data, (shape >> (7 - i)) & 0x0001 == 1,
                                  (shape >> (3 - i)) & 0x0001 == 1);
        lcd_display_bytes_in_page(page + 1, col + i * 4, two_box_display_data, sizeof(two_box_display_data));
    }
}

uchar game_eliminate_lines()
{
    uchar i, j = 0;
    // 返回值，其他方法根据返回的行数判断是否要更新速度和分数的显示
    uchar lines_count = 0;
    // 从上到下遍历游戏区
    for (i = 0; i < 16; i++)
    {
        // 填满一行的话 一行数据都是1
        if ((GAME_ZONE[i] & 0x3ffc) == 0x3ffc)
        {
            // 0填充新数据，其他数据依次向前一格
            for (j = i; j > 0; j--)
            {
                GAME_ZONE[j] = GAME_ZONE[j - 1];
            }
            GAME_ZONE[0] = 0x2004;

            // 下一次循环，需要先要判断当前移动下来的
            i--;

            game_score++;
            game_eliminate_lines_count++;
            lines_count++;
            if (game_speed < 9 && game_eliminate_lines_count % 30 == 0)
            {
                game_speed++;
            }
        }
    }
    return lines_count;
}

uint get_timer0_value()
{
    uchar high, low;
    do
    {
        high = TH0;
        low = TL0;
    } while (high != TH0);
    return (high << 8) | low;
}

void game_rand_seed_update()
{
    game_rand_seed ^= get_timer0_value();
    printf("game seed: 0x%X\n", game_rand_seed);
}

void game_data_init()
{
    uchar i;
    game_speed = 0;
    game_score = 0;
    game_eliminate_lines_count = 0;
    game_stage = GAME_STAGE_BEGIN;
    game_button_continuous_count = 0;
    game_pressed_button = PRESSED_NONE;
    game_pressed_button_acceleration = 1;
    for (i = 0; i < 19; i++)
    {
        GAME_ZONE[i] = GAME_ZONE_INIT_DATA[i];
    }
}
